package confInfo;

import javax.servlet.jsp.PageContext;
import javax.servlet.http.*;

import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLEventReader;

public class UserInfoBean {
	String startUrl;
	String pattern;
	
	final String CONFIGURE = "configure";
	final String START_URL = "startUrl";
	final String PATTERN = "pattern";
	final String CONFIGURE_FILE = "configure.xml";
	
	HttpServletRequest request;
	PageContext context;
	String basePath;
	public void setStartUrl(String s){
		this.startUrl = s;
	}
	public String getStartUrl(){
		return this.startUrl;
	}
	
	
//	public void setConfigFile(String s){
//		this.configFile = s;
//	}
//	public String getConfigFile(){
//		return this.configFile;
//	}
	
	
	public void setPattern(String s){
		this.pattern = s;
	}
	public String getPattern(){
		return this.pattern;
	}
	
	
	public void setRequest(HttpServletRequest request){
		this.request = request;
	}
	
	public void setContext(PageContext context){
		this.context = context;
		this.basePath = context.getServletContext().getRealPath("/");
	}
	
	public String getBasePath(){
		return this.basePath;
	}
	
	public boolean isXmlGenerated(){
		if(this.CONFIGURE_FILE != null&&this.pattern != null&&this.startUrl != null){
			try{saveConfig();
			}catch(Exception e){
				e.printStackTrace();
				return false;
			}
			return true;
		}
		else return false;
		
	}
	
	public void parseXml(String fileName){
		try{
			// First create a new XMLInputFactory
			XMLInputFactory inputFactory = XMLInputFactory.newInstance();
			// Setup a new eventReader
			InputStream in = new FileInputStream(this.basePath + fileName);
			XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
			while (eventReader.hasNext()){
				XMLEvent event = eventReader.nextEvent();
				if (event.isStartElement()){
					StartElement startElement = event.asStartElement();
					if (startElement.getName().getLocalPart() == (START_URL)) {
						event = eventReader.nextEvent();
						this.setStartUrl(event.asCharacters().getData());
						continue;
					}
					if (startElement.getName().getLocalPart() == (PATTERN)) {
						event = eventReader.nextEvent();
						this.setPattern(event.asCharacters().getData());
						continue;
					}
				}
				
			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("parse " + this.basePath + fileName + "error");
		}
	}
	public void saveConfig() throws Exception {
		// Create a XMLOutputFactory
		XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
		// Create XMLEventWriter
		XMLEventWriter eventWriter = outputFactory
				.createXMLEventWriter(new FileOutputStream(basePath + CONFIGURE_FILE));
		// Create a EventFactory
		XMLEventFactory eventFactory = XMLEventFactory.newInstance();
		XMLEvent end = eventFactory.createDTD("\n");
		// Create and write Start Tag
		StartDocument startDocument = eventFactory.createStartDocument();
		eventWriter.add(startDocument);

		// Create config open tag
		StartElement configStartElement = eventFactory.createStartElement("",
				"", "config");
		eventWriter.add(configStartElement);
		eventWriter.add(end);
		// Write the different nodes
		createNode(eventWriter, "startUrl", this.startUrl);
		createNode(eventWriter, "pattern", this.pattern);


		eventWriter.add(eventFactory.createEndElement("", "", "config"));
		eventWriter.add(end);
		eventWriter.add(eventFactory.createEndDocument());
		eventWriter.close();
	}

	private void createNode(XMLEventWriter eventWriter, String name,
			String value) throws XMLStreamException {

		XMLEventFactory eventFactory = XMLEventFactory.newInstance();
		XMLEvent end = eventFactory.createDTD("\n");
		XMLEvent tab = eventFactory.createDTD("\t");
		// Create Start node
		StartElement sElement = eventFactory.createStartElement("", "", name);
		eventWriter.add(tab);
		eventWriter.add(sElement);
		// Create Content
		Characters characters = eventFactory.createCharacters(value);
		eventWriter.add(characters);
		// Create End node
		EndElement eElement = eventFactory.createEndElement("", "", name);
		eventWriter.add(eElement);
		eventWriter.add(end);

	}
	
//	public static void main(String[] args) {
//		UserInfoBean configFile = new UserInfoBean();
//		configFile.setConfigFile("config2.xml");
//		configFile.setPattern("a");
//		configFile.setStartUrl("d");
//		try {
//			configFile.saveConfig();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
}
